package org.example;

import org.example.code.config.MySpringConfig;
import org.example.code.service.TestService;
import org.example.testspring.context.impl.MyAnnotationApplicationContext;
import org.example.testspring.context.impl.MyClassPathXmlApplicationContext;
import org.example.testspring.exception.NoBeanDefinedException;
import org.example.testspring.exception.NoBeanSetterException;
import org.example.testspring.exception.NoNamedBeanException;
import org.example.testspring.exception.NoSuchAttributeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @author 浆布
 */
public class App
{
    /**
     * @param args the command line argument
     */
    public static void main( String[] args )
    {

        System.out.println( "---------- Spring xml Implements ----------" );
        springImpl();
        System.out.println( "---------- My xml Implements ----------" );
        myImpl();
        System.out.println( "---------- My annotation Implements ----------" );
        myAnnotationImpl();
    }

    /**
     * Spring xml Implements
     * @return
     * @throws
     */
    public static void springImpl(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestService testService = (TestService) applicationContext.getBean("testService");
        testService.saySomething();
    }

    /**
     * My xml Implements
     * @return
     * @throws
     */
    public static void myImpl(){
        MyClassPathXmlApplicationContext applicationContext = null;
        try {
            applicationContext = new MyClassPathXmlApplicationContext("applicationContext.xml");
        } catch (NoSuchAttributeException | NoBeanDefinedException | IOException | ClassNotFoundException |
                 NoNamedBeanException | NoBeanSetterException e) {
            e.printStackTrace();
        }
        TestService testService = null;
        try {
            testService = (TestService) applicationContext.getBean("testService");
        } catch (NoNamedBeanException e) {
            throw new RuntimeException(e);
        }
        testService.sayHello();
        testService.saySomething();
        applicationContext.close();
    }

    public static void myAnnotationImpl() {
        org.example.testspring.context.ApplicationContext applicationContext = null;
        try {
            applicationContext = new MyAnnotationApplicationContext(MySpringConfig.class);
        } catch (NoNamedBeanException e) {
            throw new RuntimeException(e);
        }
        TestService testService = null;
        try {
            testService = (TestService) applicationContext.getBean("testService");
        } catch (NoNamedBeanException e) {
            throw new RuntimeException(e);
        }
        testService.sayHello();
        testService.saySomething();
        applicationContext.close();
    }
}


